//
//  AFFNavigationController.m
//  AnyfishApp
//
//  Created by Bob Lee on 14-7-25.
//  Copyright (c) 2014年 Anyfish. All rights reserved.
//

#import "AFFNavigationController.h"

@interface AFFNavigationController () 

@property (nonatomic, strong) UIColor *hideColor;
@property (nonatomic, weak) UIImageView  *viewStatusBg;

//@property (nonatomic,strong) NJKWebViewProgress     *progress;///< webview进度
//@property (nonatomic,weak ) NJKWebViewProgressView *progressView;///< webview进度条

@end

@implementation AFFNavigationController

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated{
    [super setNavigationBarHidden:hidden animated:animated];
    
    [[UIApplication sharedApplication] setStatusBarStyle:hidden ? UIStatusBarStyleDefault : UIStatusBarStyleLightContent animated:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:hidden];
    
    if(hidden){
        self.hideColor = _statusBarBgColor;
        self.statusBarBgColor = [UIColor clearColor];
    }else if(_hideColor){
        self.statusBarBgColor = [_hideColor isEqual:[UIColor clearColor]]?nil:_hideColor;
    }
}

- (void)setNavigationBarHidden:(BOOL)navigationBarHidden{
    [super setNavigationBarHidden:navigationBarHidden];
    
     [[UIApplication sharedApplication] setStatusBarStyle:navigationBarHidden ? UIStatusBarStyleDefault : UIStatusBarStyleLightContent animated:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:navigationBarHidden];

    
    self.hideColor = _statusBarBgColor;
    
    if(navigationBarHidden){
        self.hideColor = _statusBarBgColor;
        self.statusBarBgColor = [UIColor clearColor];
    }else if(_hideColor){
        self.statusBarBgColor = _hideColor;
    }
}

- (void)setNavigationBarColorStr:(NSString *)navigationBarColorStr{
    _navigationBarColorStr = navigationBarColorStr;
    ((AFFNavbar*)self.navigationBar).bgColorStr = navigationBarColorStr==nil ? [UIColor colorToString:kSetting.color_Bg_Nav] : navigationBarColorStr;
    ((AFFNavbar*)self.navigationBar).bgColorEx = (navigationBarColorStr==nil || ![navigationBarColorStr isEqualToString:[UIColor colorToString:[UIColor clearColor]]]) && kSetting.cacheMainFlag==3 ? kSetting.color_eccd8e : nil;
}

- (void)setNavigationBarColor:(UIColor *)navigationBarColor {
    _navigationBarColor = navigationBarColor;
    ((AFFNavbar*)self.navigationBar).bgColor = navigationBarColor==nil ? kSetting.color_Bg_Nav : navigationBarColor;
    ((AFFNavbar*)self.navigationBar).bgColorEx = (navigationBarColor==nil || ![navigationBarColor isEqual:[UIColor clearColor]]) && kSetting.cacheMainFlag==3 ? kSetting.color_eccd8e : nil;
}

- (void)setStatusBarBgColor:(UIColor *)statusBarBgColor{
    _statusBarBgColor = statusBarBgColor;
    self.viewStatusBg.backgroundColor = statusBarBgColor == nil ? kSetting.color_Bg_Nav : statusBarBgColor;
    
    self.viewStatusBg.image = nil;
    if(statusBarBgColor==nil && kSetting.cacheMainFlag==3)
        self.viewStatusBg.image = [UIImage imageFromColors:@[kSetting.color_Bg_Nav, kSetting.color_eccd8e] gradientType:EGradientTypeLeftToRight size:self.viewStatusBg.bounds.size];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    isPush = YES;
    viewController.hidesBottomBarWhenPushed = YES;
    viewController.tabBarItem = self.tabBarItem; // 这样二级vc就能直接得到下导航了
    [super pushViewController:viewController animated:animated];
}

- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated {
    if(viewControllers.count>1){
        isPush = YES;
        for(int i=1; i<viewControllers.count; i++){
            ((UIViewController*)[viewControllers objectWithIndex:i]).hidesBottomBarWhenPushed = YES;
            ((UIViewController*)[viewControllers objectWithIndex:i]).tabBarItem = self.tabBarItem;
        }
    }
    
    [super setViewControllers:viewControllers animated:animated];
}

- (NSArray*)popToRootViewControllerAnimated:(BOOL)animated{
    isPush = NO;
    if(self.currentShowVC){
        if(self.currentShowVC != self.visibleViewController)
            self.currentShowVC = self.visibleViewController;
        
        [self.currentShowVC resignFirstResponder];
        
        // 如果是返回root，需要清理不显示的vc
        for(UIViewController *vc in self.viewControllers){
            if(vc == [self.viewControllers firstObject]/* || vc == self.currentShowVC*/)
                continue;
            
        }
    }
    return [super popToRootViewControllerAnimated:animated];
}

- (NSArray*)popToViewController:(UIViewController *)viewController animated:(BOOL)animated{
    isPush = NO;
    if(self.currentShowVC){
        if(self.currentShowVC != self.visibleViewController)
            self.currentShowVC = self.visibleViewController;
        
        [self.currentShowVC resignFirstResponder];
        
        // 清理中间的，一定是从后面跳到前面
        NSInteger start = [self.viewControllers indexOfObject:viewController];
        NSInteger end = [self.viewControllers indexOfObject:self.currentShowVC];
        
        for(NSInteger i=(start+1); i<end; i++){
            if(i>(self.viewControllers.count-1))
                break;
            
            UIViewController *vc = [self.viewControllers objectWithIndex:i];
        }
    }
    return [super popToViewController:viewController animated:animated];
}

- (UIViewController*)popViewControllerAnimated:(BOOL)animated{
    isPush = NO;
    // 这里需要处理一下这个委托还没有调用时，用户就返回的问题
    if(self.currentShowVC && self.currentShowVC != self.visibleViewController)
        self.currentShowVC = self.visibleViewController;
    
    if(self.currentShowVC){
        [self.currentShowVC resignFirstResponder];
    }
    return [super popViewControllerAnimated:animated];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if(self.currentShowVC == viewController)
        return;
    
    if(self.currentShowVC && !isPush && self.currentShowVC != [self.viewControllers firstObject]){
        [self.currentShowVC.navigationItem setLoading:NO];
        // 由于手势返回可能currentShowVC.aNavigationController已经断开关联，所以这里用当前导航处理
        if([navigationController isKindOfClass:[AFFNavigationController class]]){
            // vc.cleanSelf已经调用，但是手势返回不会及时触发，切存在导航栏关联问题，这里补充一次
            [(AFFNavigationController*)navigationController hideProgress];
            [(AFFNavigationController*)navigationController hideBanner:NO];
        }
    }
    
    if (navigationController.viewControllers.count == 1)
        self.currentShowVC = Nil;
    else {
        self.currentShowVC = viewController;
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.delegate = self;
    self.interactivePopGestureRecognizer.enabled = YES;
    
    if(self.viewStatusBg==nil){
        // 增加状态栏背景
        UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, -kStatusBar_Height, self.navigationBar.bounds.size.width, kStatusBar_Height)];
        view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.viewStatusBg = view;
        [self.navigationBar addSubview:self.viewStatusBg];
        
        self.navigationBar.translucent = YES;
        self.navigationBar.barStyle = UIBarStyleBlack;
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }
    
    self.statusBarBgColor = self.statusBarBgColor;
    self.navigationBarColor = self.navigationBarColor;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark 进度控制
//
//- (void)showProgerss:(id<UIWebViewDelegate>)delegate webV:(UIWebView*)webV {
//    if(self.progressView==nil){
//        NJKWebViewProgressView *progressView=[[NJKWebViewProgressView alloc]initWithFrame:CGRectMake(0, kNavigationBar_Height-3, SCREEN_WIDTH, 3)];
//        progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
//        [self.navigationBar addSubview:progressView];
//        self.progressView=progressView;
//
//        self.progress=[[NJKWebViewProgress alloc]init];
//        self.progress.progressDelegate=self;
//    }
//
//    webV.delegate=self.progress;
//    self.progressView.progress = 0;
//    self.progress.webViewProxyDelegate=delegate;
//}
//
////- (void)updateProgress:(float)progress {
////    self.progressView.progress = progress;
////}
//
//- (void)hideProgress {
//    if(self.progressView){
//        self.progress.webViewProxyDelegate=nil;
//        self.progress.progressDelegate=nil;
//        [self.progressView removeFromSuperview];
//        self.progressView=nil;
//        self.progress=nil;
//        self.progressView.progress = 0;
//    }
//}
//
//- (void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress{
//    if(progress<=self.progressView.progress)
//        return;
//
//    [self.progressView setProgress:progress animated:YES];
//}

-(void)hideBanner:(BOOL)animate{

}

-(void)showBanner:(float)dispTime{
    
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return [self.currentShowVC preferredStatusBarStyle];
}

- (BOOL)prefersStatusBarHidden {
    return [self.currentShowVC prefersStatusBarHidden];
}


- (UIViewController *)childViewControllerForStatusBarStyle{
    return self.visibleViewController;
}

- (UIViewController *)childViewControllerForStatusBarHidden {
    return self.visibleViewController;
}

- (BOOL)shouldAutorotate {
    NSLog(@"self.viewControllers.lastObject %@" , self.viewControllers.lastObject);
    return [self.viewControllers.lastObject shouldAutorotate];
}

//- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    return [self.viewControllers.lastObject supportedInterfaceOrientations];
//}
//
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//    return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
//}

@end
